﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Design;

namespace _0812367_0912581
{
    public class PlayerSprite: MySprite
    {
        private bool isLiving;

        public bool IsLiving
        {
            get { return isLiving; }
            set { isLiving = value; }
        }

        private int hp;

        public int Hp
        {
            get { return hp; }
            set { hp = value; }
        }



        protected int timeSinceLastFire = 0;
        protected int timeFireDelay;


        //Bullets
        private Texture2D bulletTexture;
        private List<PlayerBullet> playerBullets = new List<PlayerBullet>();

        public List<PlayerBullet> PlayerBullets
        {
            get { return playerBullets; }
            set { playerBullets = value; }
        }

        protected bool allowFire;

        protected int BULLETSPEED = 10;

        //private ContentManager _content;

        //public ContentManager Content
        //{
        //    get { return _content; }
        //    set { _content = value; }
        //}

        public PlayerSprite(Texture2D aImage, Point aSheetsize, Vector2 thePosition, int theColOffset, float scale, int timePerFrame, int theTimeFireDelay, bool isLiving, Texture2D aBulletTexture, int theHp, bool AllowFire)
            : base(aImage, aSheetsize, thePosition, theColOffset, scale, timePerFrame)
        {
            this.isLiving = isLiving;
            this.timeFireDelay = theTimeFireDelay;
            this.bulletTexture = aBulletTexture;
            this.hp = theHp;
            this.allowFire = AllowFire;
        }

        public PlayerSprite(PlayerSprite aPlant)
            :base(aPlant)
        {
            this.isLiving = aPlant.isLiving;
            this.timeFireDelay = aPlant.timeFireDelay;
            this.bulletTexture = aPlant.bulletTexture;
            this.hp = aPlant.hp;
            this.allowFire = aPlant.allowFire;
        }

        public virtual void LoadContent(ContentManager contentMan)
        {
            //this.Content = contentMan;
            ////base.LoadContent(this.Content);


            //foreach (PlayerBullet bullet in playerBullets)
            //{
            //    bullet.LoadContent(this.Content);
            //}
        }

        public override void Update(GameTime gameTime)
        {            
            Fire(gameTime);
            UpdateHp(gameTime);
            base.Update(gameTime);


        }

        public void UpdateHp(GameTime gameTime)
        {
            if (this.hp <= 0)
                this.isLiving = false;                
        }

        public virtual void UpdatePlants()
        {

        }

        public virtual void Fire(GameTime gameTime)
        {
            if (allowFire)
            {
                if (isLiving)
                {
                    ShootAbullet(gameTime);

                    foreach (PlayerBullet bullet in playerBullets)
                    {
                        bullet.UpdateVisible(gameTime);

                    }

                    //int n = playerBullets.Count;
                    for (int i = 0; i < playerBullets.Count; i++)
                    {
                        if (playerBullets[i].Visible == false)
                        {
                            playerBullets.RemoveAt(i);
                        }
                    }

                    foreach (PlayerBullet bullet in playerBullets)
                    {
                        bullet.Update(gameTime);
                    }
                }
            }
        }

        public virtual void ShootAbullet(GameTime gameTime)
        {
            timeSinceLastFire += gameTime.ElapsedGameTime.Milliseconds;

            float x0 = this.FrameSize.X * Scale - this.CollisionOffset;
            float y0 = this.FrameSize.Y * Scale/2 - this.CollisionOffset;
            if (timeSinceLastFire > timeFireDelay)
            {
                timeSinceLastFire = 0;

                PlayerBullet bullet = new PlayerBullet(bulletTexture, new Point(1, 1), this.Position + new Vector2(x0, y0), 20, 0.3f, 20, new Vector2(this.BULLETSPEED, 0), true);
                playerBullets.Add(bullet);
                
            }
        }


        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            foreach (PlayerBullet bullet in playerBullets)
            {
                if (bullet.Visible)
                {
                    bullet.Draw(gameTime, spriteBatch);
                }
            }
            base.Draw(gameTime, spriteBatch);
        }
    }
}
